﻿using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Mapping.Attributes;
using NHMA = NHibernate.Mapping.Attributes;

namespace SuperMarketManagement.Entity
{

    [Class(Name = "Employee")]
    public class Employee
    {
        [Id(1, Name = "AutoId"), Generator(2, Class = "native")]
        public virtual long AutoId { get; set; }
        [Property]
        public virtual string Username { get; set; }
        [Property]
        public virtual string Password { get; set; }
        [Property]
        public virtual string Fullname { get; set; }
        [Property]
        public virtual Sex Sex { get; set; }
        [Property]
        public virtual DateTime DateOfBirth { get; set; }
        [Property]
        public virtual bool Enabled { get; set; }

        [NHMA.List(0, Name = "Roles", Table = "UserRole", Lazy = CollectionLazy.False, Cascade = "None")]
        [Key(1, Column = "UserAutoId")]
        [Index(2, Column = "RoleIndex")]
        [ManyToMany(3, Column = "RoleAutoId", ClassType = typeof(Role))]
        public virtual IList<Role> Roles { get; set; }

        public Employee()
        {
            Roles = new List<Role>();
            Enabled = true;
        }

        public Employee(String username, String fullname, Sex sex, DateTime dateOfBirth, String password, bool enabled, params Role[] roles)
            : this()
        {
            Username = username;
            Fullname = fullname;
            Sex = sex;
            DateOfBirth = dateOfBirth;
            Password = password;
            Enabled = enabled;
            foreach (Role r in roles)
            {
                AddRole(r);
            }
        }
        public virtual void AddRole(Role r)
        {
            Roles.Add(r);
        }

        public virtual void RemoveRole(Role r)
        {
            Roles.Remove(r);
        }

        public virtual bool HasRole(Role r)
        {
            return Roles.IndexOf(r) != -1;
        }

        public virtual void RemoveAllRoles()
        {
            Roles.Clear();
        }
    }
}